package com.whut.welcomepage.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.whut.welcomepage.MovieBean;
import com.whut.welcomepage.R;
import com.whut.welcomepage.adapter.ListViewAdapter_Main;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends Fragment {
    private Handler handler = new Handler();
    private List<MovieBean.ListBean> mList = new ArrayList<>();
    private TextView textView_pagerFragment;
    private ListView listView_pagerFragment;
    private int index;
    private String url;
    private String[] title;
    private ListViewAdapter_Main adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i("TAG", "------>onCreateView:创建");
        return inflater.inflate(R.layout.fragment_pager, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textView_pagerFragment = (TextView) view.findViewById(R.id.textView_pagerFragment);
        listView_pagerFragment = (ListView) view.findViewById(R.id.listView_pagerFragment);
        initData();
        initUI();

    }

    private void initUI() {
        textView_pagerFragment.setText(title[index]);

        adapter = new ListViewAdapter_Main(getContext(), mList);
        listView_pagerFragment.setAdapter(adapter);
    }

    private void initData() {
        title = new String[]{"天猫超市", "天猫国际", "天猫电器", "天猫汽车"};
        Bundle bundle = getArguments();
        index = bundle.getInt("index");
        url = bundle.getString("url");
        loadDataFromNet(url);
    }

    private void loadDataFromNet(final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] bytes = HttpURLConnHelper.getByteArrayFromNet(url);
                if (bytes != null) {
                    Gson gson = new Gson();
                    MovieBean movieBean = gson.fromJson(new String(bytes), MovieBean.class);

                    mList.addAll(movieBean.getList());
//mList=movieBean.getList();

//                    在主线程更新适配器数据
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();

                        }
                    });
                }
            }
        }).start();
    }

    public static BlankFragment getInstance(int index, String url) {
        BlankFragment fragment = new BlankFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        bundle.putString("url", url);
        fragment.setArguments(bundle);
        return fragment;
    }

}
